#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/utsname.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

#define BUF_SIZE 10000
char path[]={"/device/name"};
char find[]={"sudo find /sys/block/ -name \"*mmcblk*\" "};

int main()
{
	int pid,fd_pipe,ret;
    FILE * p_file_0 = NULL;
    FILE * p_file_1=NULL;

    p_file_0 = popen(find,"r");
    if(!p_file_0){
        perror("popen");
    }
    char buf[BUF_SIZE];
    int yes=0;
    char mmc[100];
    while(fgets(buf,BUF_SIZE,p_file_0)!=NULL){
        char tmp[100]={"cat "};
        strcat(tmp,buf);
        int len=strlen(tmp);
        int pos=len-1;
        while(tmp[pos]!='/')--pos;
        int i=0;++pos;
        for (i = 0; i < len-pos; ++i)
        {
            mmc[i]=tmp[pos+i];
        }
        mmc[i]='\0';
        for (i = 0; i < 12; ++i)
        {
            tmp[len-1+i]=path[i];
        }
        tmp[len-1+12]='\0';
        p_file_1 = popen(tmp,"r");
        char name[100];
        fgets(name,100,p_file_1);
        if(strstr(name,"SCY"))  //search /sys/block/mmc*/device/name
        {
            yes=1;
            break;
        }
    }
    if(yes){
        char devpath[]={"/dev/block/"};
        strcat(devpath,mmc);
        printf("\ndevpath=%s\n", devpath);
    }else{
        printf("Can not find MMC of SCY\n");
    }

    pclose(p_file_0);
    pclose(p_file_1);


	return 0;
}